import { createRouter, createWebHashHistory } from 'vue-router'
// 路由组件的按需加载（懒加载）
const Login = () => import('@/views/login/index')
const Cart = () => import('@/views/cart/index.vue')
const Callback = () => import('@/views/login/callback.vue')
const Layout = () => import('@/views/layout/index')
const Home = () => import('@/views/home/index')
const TopCategory = () => import('@/views/category')
const SubCategory = () => import('@/views/category/sub')
const Goods = () => import('@/views/goods/index.vue')

const routes = [
  // 主页面路由配置
  {
    path: '/',
    component: Layout,
    children: [
      // 主页二级路由配置
      { path: '/', component: Home },
      { path: '/category/:id', component: TopCategory },
      { path: '/category/sub/:id', component: SubCategory },
      { path: '/product/:id', component: Goods },
      { path: '/cart', component: Cart }
    ]
  },
  { path: '/login', component: Login },
  { path: '/login/callback', component: Callback }
]

// 创建路由实例
const router = createRouter({
  // 使用hash方式实现路由
  history: createWebHashHistory(),
  routes,
  scrollBehavior () {
    return { left: 0, top: 0 }
  }
})

export default router
